package com.sakura.二分答案;

public class Code875_爱吃香蕉的珂珂 {

    public int minEatingSpeed(int[] piles, int h) {
        int left = 0;
        int right = 0;
        for (int pile : piles) {
            right = Math.max(right, pile);
        }
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(piles, mid, h)) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return right;
    }

    public boolean check(int[] piles, int speed, int h) {
        long time = 0;
        for (int pile : piles) {
            time += (pile + speed - 1) / speed;
        }
        return time <= h;
    }
}
